package com.java2207.group1.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.java2207.group1.bo.StaffBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.listener.WebStaffListener;
import com.java2207.group1.pojo.Staff;
import com.java2207.group1.service.StaffService;
import com.java2207.group1.util.OssUtil;

import com.java2207.group1.vo.staff.StaffBarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author HLJ
 * @Date 2022/7/25 07
 */
@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    @RequestMapping("/staffList")
    public LayuiPageVo<Staff> staffPageDate(@RequestParam Map<String, Object> params) {
        LayuiPageVo<Staff> staffLayuiPageVo = staffService.staffPageDate(params);
        System.out.println(staffLayuiPageVo);
        return staffLayuiPageVo;
    }

    @RequestMapping("/addStaff")
    public R addStaff(Staff staff) {
        int result = staffService.addStaff(staff);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/deleteStaff")
    public R deleteStaff(Integer id) {
        int result = staffService.deleteStaff(id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/updateStaff")
    public R updateStaff(Staff staff) {
        int result = staffService.updateStaff(staff);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {

        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String url = OssUtil.uploadFile(inputStream, originalFilename);
            return R.ok().data("url", url);
        }
        return R.error();
    }

    @RequestMapping("/removeStaff")
    public R removeStaff(String ids){
        int result = staffService.removeStaff(ids);
        if (result >= 1){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @RequestMapping("/echartBar")
    public R echartBar() {
        List<StaffBarVo> list = staffService.numberEchartBar();
        return R.ok().data("numberBar", list);
    }

    @Autowired
    private WebStaffListener webStaffListener;

    @RequestMapping("export")
    public void export(Map<String,Object> params, HttpServletResponse response) throws IOException {
        // 设置表格的响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 下载的文件名 是中文 要进行转码 不然会乱码
        String filename = URLEncoder.encode("员工信息导出", "utf-8");

        // 告诉浏览器 下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        // 得到浏览器的输出流 像浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, StaffBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        // 准备满足条件的集合数据
        List<StaffBo> staffs = staffService.staffExcelData(params);
        //  把集合数据写到输出流
        sheet.doWrite(staffs);
    }

    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        // 加个判断 避免 请求出现错误
        if (file != null) {
            /**
             * file.getInputStream() 文件流
             * CommodityBo.class 读取到的数据封装到哪个数据类型中
             * webCommodityListener 监听器 每监听到一行数据 就会调用监听器中的 invoke 方法
             */
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), StaffBo.class, webStaffListener);
            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();

            // 读取sheet
            sheet.doRead();
        }
        return R.ok();
    }

    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        //得到模板文件的路径
        String realPath = request.getServletContext().getRealPath("/template/员工信息模板.xlsx");

        //根据文件路径 创建File 对象
        File file = new File(realPath);

        // 把File  对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("员工信息模板", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);

        outputStream.close();
    }

}
